{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f17e7dff",
   "metadata": {},
   "source": [
    "# python基础入门6 _ Map&Filter函数\n",
    "map()和filter()是Python内置的高阶函数，它们都接受一个函数和一个可迭代对象（如列表、元组等）作为参数。\n",
    "\n",
    "map()函数：map()函数用于映射，它将一个序列的每个元素依次传递给指定的函数进行操作，并返回一个包含所有结果的迭代器。它不会改变原数组，也不会检查空数组。\n",
    "\n",
    "filter()函数：filter()函数用于过滤序列，它接收一个函数和一个序列作为参数，然后将该函数应用于序列的每个元素，最终返回一个由所有使得函数返回值为真的元素组成的迭代器。它会根据函数中的筛选条件将返回符合条件的结果组成一个新的数组并返回。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc71be03",
   "metadata": {},
   "source": [
    "## map()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "82c72c48",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst=list(range(0,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "47960f6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ff3cfcb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def square(x):\n",
    "    return x*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ff76e57b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "square(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1d577039",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<map at 0x1caab2ab7c0>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "map(square,lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b72f7ea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(square,lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "8f704025",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x:x**2,lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edae3ed6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b948261e",
   "metadata": {},
   "source": [
    "## filter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "05c62094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst=list(range(0,10))\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "caaae7ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_even(x):\n",
    "    return x % 2 == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e26a7d5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(filter(is_even,lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0bab18e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "419ffada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, False, True, False, True, False, True, False, True, False]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(is_even,lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d831d837",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(filter(lambda x:x%2==0,lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08a60ce1",
   "metadata": {},
   "outputs": [],
   "source": [
    "filter()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
